<template>
  <el-dialog :title="dialogForm.id? '编辑角色':'添加角色'" :visible="showDialog" @close="cancel">
    <el-form ref="form" :model="dialogForm" :rules="rules" label-width="80px">
      <el-form-item label="标题" prop="title">
        <el-input v-model="dialogForm.title" />
      </el-form-item>
      <el-form-item label="描述" prop="description">
        <!-- resize:none去除右下角 -->
        <el-input
          v-model="dialogForm.description"
        />
      </el-form-item>
    </el-form>
    <span slot="footer">
      <el-button type="primary" size="small" @click="confirm">确 定</el-button>
      <el-button size="small" @click="cancel">取 消</el-button>
    </span>
  </el-dialog>
</template>

<script>
import { addRole, editRole } from '@/api/role'

export default {
  props: {
    showDialog: {
      type: Boolean,
      default: false
    }
  },
  data() {
    return {
      dialogForm: {
        title: '',
        description: ''
      },
      rules: {
        title: [{ required: true, message: '该项不能为空', trigger: 'blur' }],
        description: [{ required: true, message: '该项不能为空', trigger: 'blur' }]
      }
    }
  },
  methods: {
    async confirm() {
      await this.$refs.form.validate()
      if (this.dialogForm.id) {
        await editRole(this.dialogForm)
        this.$message.success('修改成功')
      } else {
        await addRole(this.dialogForm)
        this.$message.success('添加成功')
      }
      this.$emit('reload')
      this.$emit('close')
    },
    cancel() {
      this.$refs.form.clearValidate()
      this.$emit('close')
      this.dialogForm = {}
    }
  }
}
</script>

<style>

</style>
